package live.zs.elasticsearchdata.controller;

import live.zs.elasticsearchdata.entity.es.EsBlog;
import live.zs.elasticsearchdata.entity.mysql.MysqlBlog;
import live.zs.elasticsearchdata.repository.es.EsBlogRepository;
import live.zs.elasticsearchdata.repository.mysql.MysqlBlogRepository;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Optional;

/**
 * @author madison
 * @description
 * @date 2021/3/4 9:59 下午
 */
@RestController
@Slf4j
public class DataController {

    @Autowired
    private MysqlBlogRepository mysqlBlogRepository;
    @Autowired
    private EsBlogRepository esBlogRepository;
    @Autowired
    private ElasticsearchOperations elasticsearchOperations;

    @GetMapping("/blogs")
    public Object blog() {
        List<MysqlBlog> mysqlBlogs = mysqlBlogRepository.queryAll();
        return mysqlBlogs;
    }

    @PostMapping("/search")
    public Object search(@RequestBody Param param) {
        HashMap<String, Object> map = new HashMap<>();
        StopWatch watch = new StopWatch();
        watch.start();
        String type = param.getType();
        if (type.equals("mysql")) {
            // mysql
            List<MysqlBlog> queryBlogs = mysqlBlogRepository.queryBlogs(param.getKeyword());
            map.put("list", queryBlogs);
        } else if (type.equals("es")) {
            // es
            //POST /blog/_search
            //{
            //  "query": {
            //    "bool": {
            //      "should": [
            //        {
            //          "match_phrase": {
            //            "title": "springboot"
            //          }
            //        },
            //        {
            //          "match_phrase": {
            //            "content": "springboot"
            //          }
            //        }
            //      ]
            //    }
            //  }
            //}
//            BoolQueryBuilder builder = QueryBuilders.boolQuery();
//            builder.should(QueryBuilders.matchPhraseQuery("title", param.getKeyword()));
//            builder.should(QueryBuilders.matchPhraseQuery("content", param.getKeyword()));
//            String string = builder.toString();
//            log.info(string);
//            Page<EsBlog> page = (Page<EsBlog>) esBlogRepository.search(builder);
//            map.put("list", page.getContent());

//            NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder().withQuery(builder);
//            SearchHits<EsBlog> search = elasticsearchRestTemplate.search(nativeSearchQueryBuilder.build(), EsBlog.class);
            SearchHits<EsBlog> search = elasticsearchOperations.search(Query.findAll(), EsBlog.class);
            map.put("list", search.getSearchHits().stream().map(SearchHit::getContent).toList());
        } else {
            return "i don't understand";
        }
        watch.stop();
        long totalTimeMillis = watch.getTotalTimeMillis();
        map.put("duration", totalTimeMillis);
        return map;
    }

    @GetMapping("/blog/{id}")
    public Object blog(@PathVariable("id") Integer id) {
        Optional<MysqlBlog> byId = mysqlBlogRepository.findById(id);
        return byId.get();
    }

    @Data
    public static class Param {
        // mysql, es
        private String type;
        private String keyword;
    }
}
